package com.example.myapplication;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication.fragment.BallFragment;
import com.example.myapplication.fragment.FragmentAdapter;
import com.example.myapplication.fragment.LoginFragment;
import com.example.myapplication.fragment.WeatherFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class ViewPageActivity extends AppCompatActivity {
    private List<Fragment> viewList=null;
    private final String[] TITLES={"登录","天气","缩放"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_view_page);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initViews();
    }
    private void initViews(){
        TabLayout tabLayout =findViewById(R.id.tabLayout);
        viewList=new ArrayList<>();
        viewList.add(new LoginFragment());
        viewList.add(new WeatherFragment());
        viewList.add(new BallFragment());
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), getLifecycle(), viewList);
        ViewPager2 view =findViewById(R.id.viewPager2);
        view.setUserInputEnabled(false);
        view.setAdapter(fragmentAdapter);
        new TabLayoutMediator(tabLayout, view, (tab, position) -> {
            tab.setText(TITLES[position]);
        }).attach();
    }
}